
//  Created by 莫瑞权 on 2022/5/2.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFDrawingToolsButton: UIButton {
    @objc public let avatar = UIImageView()
    //背景选中
    @objc public let selectedView = UIView()

    override func layoutSubviews() {
        super.layoutSubviews()
        var imageViewW:CGFloat = 0.0
        
        var selectedViewW:CGFloat = 31.0
        
        var   IPad:Bool = false
        if (UIDevice.current.userInterfaceIdiom == .phone) {
      
            imageViewW = 22.0
            selectedViewW = 29.0
        }else{
            imageViewW = 30.0
            selectedViewW = 40.0
            IPad = true
        }
        
        var imageViewH = imageViewW
        let selectedViewH = selectedViewW
        
        if( self.tag < 17 ){
            //
            let imageViewX = (self.frame.size.width - imageViewW) / 2.0;
            let imageViewY = (self.frame.size.height - imageViewH) / 2.0;
            
            self.avatar.frame = CGRect(x: imageViewX, y: imageViewY, width: imageViewW, height: imageViewH)
           
            
            let mm:Int = self.tag
            
            if   (mm == 0){
                self.avatar.image = UIImage.init(named: "HTLive.bundle/S")
                
          
                
            }
            else  if (mm == 1){
                self.avatar.image = UIImage.init(named: "HTLive.bundle/line")
               
            }
            else  if (mm == 2){
                
                self.avatar.image = UIImage.init(named: "HTLive.bundle/xuxian")
               
            }
            else  if (mm == 3){
                self.avatar.image = UIImage.init(named: "HTLive.bundle/zhengfang")
               
                
            }
            else  if (mm == 4){
                self.avatar.image = UIImage.init(named: "HTLive.bundle/tuoyuan1")
               
                
            }
            else {
                
                if (mm == 5){
                    self.avatar.backgroundColor = UIColor(red: 0.99, green: 0.18, blue: 0.02, alpha: 1)
                    
                }
                else if (mm == 6){
                   
                    self.avatar.backgroundColor = UIColor(red: 1, green: 0.44, blue: 0, alpha: 1)
                }
                else if (mm == 7){
                   
                    self.avatar.backgroundColor = UIColor(red: 0.98, green: 0.84, blue: 0.02, alpha: 1)
                    
                }
                else if (mm == 8){
                    
                    self.avatar.backgroundColor = UIColor(red: 0.58, green: 0.81, blue: 0.27, alpha: 1)
                }
                else if (mm == 9) {
                    
                    self.avatar.backgroundColor = UIColor(red: 0, green: 0.84, blue: 0.81, alpha: 1)
                    
                }
                else if (mm == 10){
                 
                    self.avatar.backgroundColor = UIColor(red: 0, green: 0.59, blue: 0.98, alpha: 1)
                    
                }
                else if (mm == 11){
                    
                    self.avatar.backgroundColor = UIColor(red: 0.55, green: 0.42, blue: 0.77, alpha: 1)
                    
                }
                else if (mm == 12){
                    
                    self.avatar.backgroundColor = UIColor(red: 0.99, green: 0.62, blue: 0.61, alpha: 1)
                    
                }
                else if (mm == 13){
                    
                    self.avatar.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
                    
                }
                else if (mm == 14){
                    
                    self.avatar.backgroundColor = UIColor(red: 0.61, green: 0.61, blue: 0.61, alpha: 1)
                    
                }
                else if (mm == 15){
                   
                    self.avatar.backgroundColor = UIColor(red: 0.3, green: 0.3, blue: 0.39, alpha: 1)
                }
                else if (mm == 16){
                    
                    self.avatar.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
                    
                }
                
                
            }
        }else{
            self.avatar.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
           
            
            if (IPad)
            {
                if( self.tag == 17){
                    
                    imageViewW = 12.0;
                }
                else   if (self.tag == 18){
                    imageViewW = 17.0;
                }
                else  if (self.tag == 19){
                    imageViewW = 20.0;
                }
                else   if (self.tag == 20){
                    imageViewW = 26.0;
                }
                else   if (self.tag == 21){
                    imageViewW = 30.0;
                }
            }else{
                if (self.tag == 17){
                    
                    imageViewW = 6.0;
                }
                else   if (self.tag == 18){
                    imageViewW = 10.0;
                }
                else  if (self.tag == 19){
                    imageViewW = 14.0;
                }
                else   if (self.tag == 20){
                    imageViewW = 17.0;
                }
                else   if (self.tag == 21){
                    imageViewW = 22.0;
                }
            }
            
            
            
            imageViewH = imageViewW;
            let imageViewX = (self.frame.size.width-imageViewW) / 2.0;
            let imageViewY = (self.frame.size.height-imageViewH) / 2.0;
            
            self.avatar.frame = CGRect(x: imageViewX, y: imageViewY, width: imageViewW, height: imageViewH)
           
            self.avatar.layer.cornerRadius = (imageViewW) / 2;
            
        }
        
        
        
        let selectedViewX = (self.avatar.frame.size.width-selectedViewW) / 2.0;
        let selectedViewY = (self.avatar.frame.size.height-selectedViewH) / 2.0;
        
        self.selectedView.frame = CGRect(x: selectedViewX, y: selectedViewY, width: selectedViewW, height: selectedViewH)
       
        
        self.selectedView.backgroundColor = UIColor.clear
        
        self.selectedView.layer.borderWidth = 1
        
        self.selectedView.layer.borderColor = UIColor.white.cgColor
        self.selectedView.layer.cornerRadius = 4
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.clear
           //背景色
        self.addSubview(self.avatar)
       //选中的
        self.selectedView.isHidden = true
        self.avatar.addSubview(self.selectedView)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
